package com.sh.rbac.core.security.service;

import com.sh.rbac.core.security.util.SecurityHolderUtils;
import com.sh.rbac.domain.entity.User;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * 权限服务
 *
 * @author wb
 * @since 2025-10-13
 */

@Service("ps")
public class PermissionService {

    /**
     * 所有权限标识
     */
    public static final String ALL_PERMISSION = "*:*:*";

    /**
     * 判断用户是否具有指定权限
     *
     * @param permission 权限
     * @return true/false
     */
    public boolean hasPerm(String permission) {
        if (permission.isEmpty()) {
            return false;
        }
        User loginUser = SecurityHolderUtils.getLoginUser();
        Set<String> permissions = loginUser.getPermissions();

        // 管理员拥有所有权限
        if (SecurityHolderUtils.isAdmin()) {
            return true;
        }

        // 如果包含所有权限
        if (permissions.contains(ALL_PERMISSION)) {
            return true;
        }

        // 判断权限
        return permissions.contains(permission);
    }

}
